//Wed Sep 21 09:02:50 CDT 2011
import java.util.*;
import java.util.regex.*;
import java.text.*;
import java.math.*;
import java.awt.geom.*;

public class ChangingSounds
{
	public int[] list;
	public int mmax;
	public Integer[][] mark;
	public int maxFinal(int[] changeIntervals, int beginLevel, int maxLevel)
	{
		mmax = maxLevel;
		list = changeIntervals;
		mark = new Integer[list.length+2][mmax+1];
		return solve(0, beginLevel);
	}

	public int solve(int index, int level){
		if(level > mmax) return -1;
		if(level < 0) return -1;
		if(index == list.length) return level;
		if(mark[index][level] != null) return mark[index][level];
		int ret = -1;
		ret = Math.max(ret, solve(index+1, level + list[index]));
		ret = Math.max(ret, solve(index+1, level - list[index]));
		return mark[index][level] = ret;
	}
	
	
}
//Powered by [KawigiEdit] 2.0!
